package com.roger.cmdkit.cases;

import com.roger.cmdkit.base.CmdBase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;

public class Download implements CmdBase {

    @SuppressWarnings("ResultOfMethodCallIgnored")
    @Override
    public void cmdS(String[] args){
        try {
            String path = System.getProperty("user.dir")+"\\Download";
            File f = new File(path);
            if(!f.exists()) f.mkdirs();
            if(args[1].startsWith("http")){
                String fileName = args[1].split("/")[args[1].split("/").length-1];
                URL url = new URL(args[1]);
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setConnectTimeout(5000);
                conn.setUseCaches(false);
                long size = conn.getContentLength();
                if (size > 0) {
                    InputStream is = conn.getInputStream();
                    FileOutputStream fos = new FileOutputStream(path+"\\"+fileName);
                    writeFile(size,is,fos);
                }
            }else if(args[1].startsWith("\\")){
                String fileName = args[1].split("\\\\")[args[1].split("\\\\").length-1];
                File file = new File(args[1]);
                if(file.exists())  {
                    long size = file.length();
                    InputStream is = new FileInputStream(file);
                    FileOutputStream fos = new FileOutputStream(path+"\\"+fileName);
                    writeFile(size,is,fos);
                }else{
                    System.out.println("file not found");
                }
            }else{
                System.out.println("wtk.exe: -d not support the url/address");
            }
        }catch (Throwable e){
            System.out.println("wtk.exe: -d requires an right url/address");
        }
    }

    private void writeFile(long size,InputStream is,FileOutputStream fos){
        try {
            byte[] bytes = new byte[1024*4];
            int len;
            long total=0;
            System.out.println("Download begin");
            int change=-1;
            while ((len = is.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
                total+=len;
                //print progress
                int progress= (int)((float) total/size*100);
                if(progress>change){
                    change=progress;
                    DecimalFormat df = new DecimalFormat("000");
                    System.out.print(df.format(progress) + "%");
                    for(int i=0;i<4;i++){
                        System.out.print("\b");
                    }
                }
            }
            System.out.println("Download success");
            fos.close();
            is.close();
        }catch (Throwable e){
            System.out.println("Download fail");
        }
    }

}
